package light.head.framework.editor;

import java.beans.PropertyEditorSupport;
import java.util.Date;

import light.head.constant.Sys;
import light.head.framework.utils.DateUtils;

public class CustomDatetimeEditor extends PropertyEditorSupport {
	public CustomDatetimeEditor() {
	}

	public void setAsText(String text) throws IllegalArgumentException {
		if (null == text || Sys.NONE.equals(text)) {
			setValue(null);
		} else {
			int len = text.length();
			int mark_1 = text.indexOf('-');
			int mark_2 = text.indexOf(' ');
			int mark_3 = text.indexOf(':');
			if ((len == 19 || len == 16) && mark_1 != -1 && mark_2 != -1 && mark_3 != -1) {
				if (len == 19)
					setValue(DateUtils.getDateByStr(text, DateUtils.yyyy_MM_dd_HH_mm_ss));
				if (len == 16)
					setValue(DateUtils.getDateByStr(text, DateUtils.yyyy_MM_dd_HH_mm));
				
			} else if ((len == 14 || len == 12) && mark_1 == -1 && mark_2 == -1 && mark_3 == -1) {
				if (len == 14)
					setValue(DateUtils.getDateByStr(text, DateUtils.yyyyMMddHHmmss));
				if (len == 12)
					setValue(DateUtils.getDateByStr(text, DateUtils.yyyyMMddHHmm));
			} else if (len == 10 && mark_1 != -1) {
				setValue(DateUtils.getDateByStr(text, DateUtils.yyyy_MM_dd));
			} else if (len == 8 && mark_1 == -1 && mark_2 == -1 && mark_3 == -1) {
				setValue(DateUtils.getDateByStr(text, DateUtils.yyyyMMdd));
			} else 
				setValue(null);
		}
	}

	public String getAsText() {
		Date value = (Date)getValue();
		return DateUtils.getDateStr(value, DateUtils.yyyy_MM_dd_HH_mm_ss);
	}
}
